package com.gxkj.drm.util;


import com.gxkj.drm.entitys.PushUser;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.method.HandlerMethod;

import java.lang.annotation.Annotation;

/**
 * 访问权限工具类
 * Created by changxiangyang on 2017/10/2.
 */
public class AccessUtil {

    /**
     * 用户是否有权限访问该资源
     * @param user
     * @param handler
     * @return
     */
    public static boolean checkPermission(PushUser user, final Object handler) {
//        if (handler instanceof HandlerMethod) {
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            Permission p = getAnnotation(handlerMethod, Permission.class);
//            if (user == null) {
//                // 接口是否定义不需要登录访问
//                return p!=null && !p.needLogin();
//            }
//            if (p != null) {
//                RoleType role = RoleType.find(user.getLevel());
//                // 用户需要有受访问资源的等级才能访问
//                return (role != null) && (role.getLevel() <= p.role().getLevel());
//            }
//        }
        return true;
    }


    private static  <T extends Annotation> T getAnnotation(HandlerMethod handlerMethod, Class<T> clazz) {
        T annotation = handlerMethod.getMethodAnnotation(clazz);
        if (annotation != null) {
            return annotation;
        }
        annotation = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), clazz);
        return annotation;
    }

}
